package com.zhou;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.Map;

/**
 * @Author: zhouweixin
 * @Description:
 * @Date: Created in 12:15 2018/10/26
 * @Modified By:
 */
public class Patent {
    // 标题
    private String title = "";
    // 专利类型
    private String patentType = "";
    // 申请/专利号
    private String appNum = "";
    // 申请日期
    private String appDate = "";
    // 公开/公告号
    private String pubNum = "";
    // 公开/公告日
    private String pubDate = "";
    // 主分类号
    private String mainClassCode = "";
    // 分类号
    private String classCode = "";
    // 申请/专利权人
    private String applicantName = "";
    // 发明人/设计人
    private String invName = "";
    // 申请人地址
    private String appAddress = "";
    //    国别省市代码
    private String appAreaCode = "";
    // 法律状态
    private String legalStatus = "";

    public Patent(Map map){
        this.title = map.getOrDefault("title", "").toString();
        this.patentType = map.getOrDefault("patent_type", "").toString();
        this.appNum = map.getOrDefault("app_num", "").toString();
        this.appDate = map.getOrDefault("app_date02", "").toString();
        this.pubNum = map.getOrDefault("pub_num", "").toString();
        this.pubDate = map.getOrDefault("pub_date", "").toString();
        this.mainClassCode = map.getOrDefault("main_classcode", "").toString();
        this.classCode = map.getOrDefault("class_code", "").toString();
        this.applicantName = map.getOrDefault("applicant_name", "").toString();
        this.invName = map.getOrDefault("inv_name", "").toString();
        this.appAddress = map.getOrDefault("app_address", "").toString();
        this.appAreaCode = map.getOrDefault("app_area_code", "").toString();
        this.legalStatus = map.getOrDefault("legal_status", "").toString();

        processFields();
    }

    public void processFields(){
        this.title = processArray(this.title);
        this.patentType = processArray(this.patentType);
        this.appNum = processArray(this.appNum);
        this.appDate = processArray(this.appDate);
        this.pubNum = processArray(this.pubNum);
        this.pubDate = processArray(this.pubDate);
        this.mainClassCode = processArray(this.mainClassCode);
        this.classCode = processArray(this.classCode);
        this.applicantName = processArray(this.applicantName);
        this.invName = processArray(this.invName);
        this.appAddress = processArray(this.appAddress);
        this.appAreaCode = processArray(this.appAreaCode);
        this.legalStatus = processArray(this.legalStatus);
    }

    /**
     * 处理数组
     *
     * @param text
     * @return
     */
    public String processArray(String text){
        if(text.contains("[") && text.contains("]")){
            try {
                JSONArray jsonArray = (JSONArray) JSON.parse(text);
                String[] strings = jsonArray.toArray(new String[1]);

                if(strings.length > 0){
                    String result = "";
                    result += strings[0];
                    for (int i = 1; i < strings.length; i++) {
                        result += ";" + strings[i];
                    }
                    return result;
                }
            } catch (Exception e){
            }
        }

        return text;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPatentType() {
        return patentType;
    }

    public void setPatentType(String patentType) {
        this.patentType = patentType;
    }

    public String getAppNum() {
        return appNum;
    }

    public void setAppNum(String appNum) {
        this.appNum = appNum;
    }

    public String getAppDate() {
        return appDate;
    }

    public void setAppDate(String appDate) {
        this.appDate = appDate;
    }

    public String getPubNum() {
        return pubNum;
    }

    public void setPubNum(String pubNum) {
        this.pubNum = pubNum;
    }

    public String getPubDate() {
        return pubDate;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }

    public String getMainClassCode() {
        return mainClassCode;
    }

    public void setMainClassCode(String mainClassCode) {
        this.mainClassCode = mainClassCode;
    }

    public String getClassCode() {
        return classCode;
    }

    public void setClassCode(String classCode) {
        this.classCode = classCode;
    }

    public String getApplicantName() {
        return applicantName;
    }

    public void setApplicantName(String applicantName) {
        this.applicantName = applicantName;
    }

    public String getInvName() {
        return invName;
    }

    public void setInvName(String invName) {
        this.invName = invName;
    }

    public String getAppAddress() {
        return appAddress;
    }

    public void setAppAddress(String appAddress) {
        this.appAddress = appAddress;
    }

    public String getAppAreaCode() {
        return appAreaCode;
    }

    public void setAppAreaCode(String appAreaCode) {
        this.appAreaCode = appAreaCode;
    }

    public String getLegalStatus() {
        return legalStatus;
    }

    public void setLegalStatus(String legalStatus) {
        this.legalStatus = legalStatus;
    }

    @Override
    public String toString() {
        return "Patent{" +
                "title='" + title + "\n\'" +
                ", patentType='" + patentType + "\n\'" +
                ", appNum='" + appNum + "\n\'" +
                ", appDate='" + appDate + "\n\'" +
                ", pubNum='" + pubNum + "\n\'" +
                ", pubDate='" + pubDate + "\n\'" +
                ", mainClassCode='" + mainClassCode + "\n\'" +
                ", classCode='" + classCode + "\n\'" +
                ", applicantName='" + applicantName + "\n\'" +
                ", invName='" + invName + "\n\'" +
                ", appAddress='" + appAddress + "\n\'" +
                ", appAreaCode='" + appAreaCode + "\n\'" +
                ", legalStatus='" + legalStatus + "\n\'" +
                '}';
    }
}
